package scau.mis4.chanyulin.web;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ExtendedModelMap;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import scau.mis4.chanyulin.base.common.BaseController;
import scau.mis4.chanyulin.entity.GoodDetail;
import scau.mis4.chanyulin.entity.Stock;
import scau.mis4.chanyulin.service.IGoodDetailService;
import scau.mis4.chanyulin.service.IStockService;
import java.util.List;

/**
 * Created by cyl on 2018/03/27.
 * GoodDetail Controller层
 */
@RestController
@RequestMapping("/gooddetail")
public class GoodDetailController extends BaseController {

    @Autowired
    private IGoodDetailService goodDetailService;
    @Autowired
    private IStockService stockService;

    /**
     * 添加GoodDetail
     * @param goodDetail
     * @return
     * @throws Exception
     */
    @PostMapping("/add")
    public String addGoodDetail(String goodId, GoodDetail goodDetail, Stock stock) throws Exception {
        stockService.add(stock);
        goodDetail.setStockId(stock.getId());
        goodDetail.setGoodId(goodId);
        goodDetailService.add(goodDetail);
        return createSuccessJSON();
    }

    /**
     * 删除某个GoodDetail
     * @param id
     * @return
     * @throws Exception
     */
    @PostMapping("/delete")
    public String deleteGoodDetailById(String id) throws Exception {
        goodDetailService.delete(id);
        return createSuccessJSON();
    }

    /**
     * 查找某个GoodDetail
     * @param id
     * @return
     * @throws Exception
     */
    @GetMapping("/get_by_id")
    public String getGoodDetailById(String id) throws Exception {
        GoodDetail goodDetail = goodDetailService.findByKey(id);
        Model model = new ExtendedModelMap().addAttribute("result", goodDetail);
        return createSuccessJSON(model);
    }

    /**
     * 通过good_id查找某个GoodDetail
     * @param goodId
     * @return
     * @throws Exception
     */
    @GetMapping("/get_by_goodid")
    public String getGoodDetailByGoodId(String goodId) throws Exception {
        List<GoodDetail> goodDetailList = goodDetailService.findByGoodId(goodId);
        Model model = new ExtendedModelMap().addAttribute("result", goodDetailList);
        return createSuccessJSON(model);
    }

    /**
     * 获取全部GoodDetail
     * @return
     * @throws Exception
     */
    @GetMapping("/list")
    public String listAllGoodDetail() throws Exception {
        List<GoodDetail> goodDetailList = goodDetailService.findAll();
        Model model = new ExtendedModelMap().addAttribute("result", goodDetailList);
        return createSuccessJSON(model);
    }

    /**
     * 分页查询GoodDetail
     * @param pageNum 页码
     * @param pageSize 每页数据数量
     * @return
     * @throws Exception
     */
    @PostMapping("/page")
    public String listGoodDetailByPage(int pageNum, int pageSize) throws Exception {
        PageHelper.startPage(pageNum, pageSize);
        List<GoodDetail> goodDetailList = goodDetailService.findAll();
        PageInfo<GoodDetail> pageInfo = new PageInfo<>(goodDetailList);
        Model model = new ExtendedModelMap()
                .addAttribute("result", goodDetailList)
                .addAttribute("pageInfo", pageInfo);
        return createSuccessJSON(model);
    }

    /**
     * 更新某个GoodDetail
     * @param goodDetail
     * @return
     * @throws Exception
     */
    @PostMapping("/update")
    public String updateGoodDetailById(GoodDetail goodDetail) throws Exception {
        goodDetailService.updateById(goodDetail);
        return createSuccessJSON();
    }

}
